package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.wirecutting.WireCuttingRecordAddRequest;
import com.yupi.springbootinit.model.dto.wirecutting.WireCuttingRecordQueryRequest;
import com.yupi.springbootinit.model.dto.wirecutting.WireCuttingRecordUpdateRequest;
import com.yupi.springbootinit.model.vo.WireCuttingRecordVO;
import com.yupi.springbootinit.service.ProcessSheetService;
import com.yupi.springbootinit.service.WireCuttingRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 线切割记录控制器
 */
@RestController
@RequestMapping("/wireCuttingRecord")
@Slf4j
public class WireCuttingRecordController {

    @Resource
    private WireCuttingRecordService wireCuttingRecordService;

    @Resource
    private ProcessSheetService processSheetService;

    /**
     * 根据产品名称查询工艺单信息
     */
    @GetMapping("/queryProcessSheet")
    @Operation(summary = "根据产品名称查询工艺单号")
    public BaseResponse<Map<String, Object>> queryProcessSheetByProductName(@RequestParam String productName) {
        Map<String, Object> result = processSheetService.queryProcessSheetByProductName(productName);
        return ResultUtils.success(result);
    }

    /**
     * 添加线切割记录
     */
    @PostMapping("/add")
    @Operation(summary = "添加线切割记录")
    public BaseResponse<Long> addRecord(@RequestBody WireCuttingRecordAddRequest addRequest) {
        if (addRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long recordId = wireCuttingRecordService.addRecord(addRequest);
        return ResultUtils.success(recordId);
    }

    /**
     * 更新线切割记录
     */
    @PostMapping("/update")
    @Operation(summary = "更新线切割记录")
    public BaseResponse<Boolean> updateRecord(@RequestBody WireCuttingRecordUpdateRequest updateRequest) {
        if (updateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = wireCuttingRecordService.updateRecord(updateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除线切割记录
     */
    @PostMapping("/delete")
    @Operation(summary = "删除线切割记录")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteRecord(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = wireCuttingRecordService.deleteRecord(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    /**
     * 根据ID获取线切割记录详情
     */
    @GetMapping("/get")
    @Operation(summary = "根据ID获取线切割记录详情")
    public BaseResponse<WireCuttingRecordVO> getRecordById(@RequestParam Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        WireCuttingRecordVO record = wireCuttingRecordService.getRecordById(id);
        return ResultUtils.success(record);
    }

    /**
     * 分页查询线切割记录
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询线切割记录")
    public BaseResponse<Page<WireCuttingRecordVO>> pageRecords(@RequestBody WireCuttingRecordQueryRequest queryRequest) {
        if (queryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<WireCuttingRecordVO> page = wireCuttingRecordService.pageRecords(queryRequest);
        return ResultUtils.success(page);
    }
}